package com.tsd.system.service.impl;

import com.tsd.core.constants.Constants;
import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysBugWorksheetMapper;
import com.tsd.system.entity.SysBugWorksheet;
import com.tsd.system.entity.SysBugWorksheetExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysBugWorksheetService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * 描述：SysBugWorksheet 服务实现层
 *
 * @author david
 * @date 2022/09/27 17:58:22
 */
@Service
public class SysBugWorksheetServiceImpl extends BaseServiceImpl implements SysBugWorksheetService {

    @Resource
    private SysBugWorksheetMapper sysBugWorksheetMapper;

    @Override
    public SysBugWorksheet getSysBugWorksheetById(Long id) throws Exception {
        return sysBugWorksheetMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysBugWorksheetExt queryBySid(String sid) throws Exception {
        return sysBugWorksheetMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysBugWorksheets(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            SysBugWorksheet old = sysBugWorksheetMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            List<SysBugWorksheetExt> extList = sysBugWorksheetMapper.selectSysBugWorksheetsByParentSid(old.getSid());
            for (SysBugWorksheetExt worksheetExt : extList) {
                super.autoInjectBaseData(worksheetExt, opUser, TYPE_DELETE);
                sysBugWorksheetMapper.updateByPrimaryKeySelective(worksheetExt);
            }
            sysBugWorksheetMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveSysBugWorksheet(SysBugWorksheet record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            if (HlpUtils.isEmpty(record.getParent_sid()) || Constants.ROOT_PARENT_SID.equals(record.getParent_sid())) {
                record.setParent_sid(Constants.ROOT_PARENT_SID);
            } else {
                if (HlpUtils.isEmpty(record.getParent_sid())) {
                    throw new HlpException("问题工单sid不能为空！");
                }
                SysBugWorksheetExt worksheetExt = sysBugWorksheetMapper.selectBySid(record.getParent_sid());
                if (worksheetExt == null) {
                    throw new HlpException("问题工单sid：" + record.getParent_sid() + "记录找不到！");
                }
                if (HlpUtils.isEmpty(worksheetExt.getAccount_sid())) {
                    throw new HlpException("问题工单sid：" + record.getParent_sid() + "账套不能为空！");
                }
                if (SysBugWorksheetExt.STATE_WAIT == record.getState()) {
                    worksheetExt.setState(SysBugWorksheetExt.STATE_WAIT);
                } else if (SysBugWorksheetExt.STATE_DOING == record.getState()) {
                    worksheetExt.setState(SysBugWorksheetExt.STATE_WAIT);
                } else if (SysBugWorksheetExt.STATE_SYSTEM_CLOSE == record.getState() || SysBugWorksheetExt.STATE_CLIENT_CLOSE == record.getState()) {
                    if (DataSourceContextHolder.DEFAULT_ACCOUNT_SET_SID.equals(worksheetExt.getAccount_sid())) {
                        worksheetExt.setState(SysBugWorksheetExt.STATE_SYSTEM_CLOSE);
                        record.setData_type(SysBugWorksheetExt.DATA_TYPE_SYSTEM);
                    }
                }
                autoInjectBaseData(worksheetExt, opUser, TYPE_UPDATE);
                sysBugWorksheetMapper.updateByPrimaryKeySelective(worksheetExt);
                record.setState(null);
            }
            super.autoInjectBaseData(record, opUser, TYPE_CREATE);
            sysBugWorksheetMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, TYPE_UPDATE);
            sysBugWorksheetMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<SysBugWorksheetExt> findSysBugWorksheets(Map<String, Object> params, PageBean page) throws Exception {
        return sysBugWorksheetMapper.selectByPage(params, page);
    }

    @Override
    public List<SysBugWorksheetExt> findSysBugWorksheets(Map<String, Object> params) throws Exception {
        return sysBugWorksheetMapper.selectByParams(params);
    }

    @Override
    public List<SysBugWorksheetExt> findSysBugWorksheetsByParentSid(String parent_sid) {
        return sysBugWorksheetMapper.selectSysBugWorksheetsByParentSid(parent_sid);
    }

    @Override
    public void handleSysBugWorksheet(SysBugWorksheet record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getParent_sid())) {
            throw new HlpException("问题工单sid不能为空！");
        }
        SysBugWorksheetExt worksheetExt = sysBugWorksheetMapper.selectBySid(record.getParent_sid());
        if (worksheetExt == null) {
            throw new HlpException("问题工单sid：" + record.getParent_sid() + "记录找不到！");
        }
        if (HlpUtils.isEmpty(worksheetExt.getAccount_sid())) {
            throw new HlpException("问题工单sid：" + record.getParent_sid() + "账套不能为空！");
        }
        if (SysBugWorksheetExt.STATE_WAIT == record.getState()) {
            worksheetExt.setState(SysBugWorksheetExt.STATE_WAIT);
        } else if (SysBugWorksheetExt.STATE_DOING == record.getState()) {
            worksheetExt.setState(SysBugWorksheetExt.STATE_WAIT);
        } else if (SysBugWorksheetExt.STATE_SYSTEM_CLOSE == record.getState() || SysBugWorksheetExt.STATE_CLIENT_CLOSE == record.getState()) {
            if (DataSourceContextHolder.DEFAULT_ACCOUNT_SET_SID.equals(worksheetExt.getAccount_sid())) {
                worksheetExt.setState(SysBugWorksheetExt.STATE_SYSTEM_CLOSE);
            }
        }
        autoInjectBaseData(worksheetExt, opUser, TYPE_UPDATE);
        sysBugWorksheetMapper.updateByPrimaryKeySelective(worksheetExt);
        autoInjectBaseData(record, opUser, TYPE_CREATE);
        sysBugWorksheetMapper.insertSelective(record);
    }
}
